package com.gydz.gydzbaseframeworkboot.common.config.druid;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.gydz.gydzbaseframeworkboot.wgCenter.dao", sqlSessionFactoryRef = "InfAlarmSqlSessionFactory")
public class WgmasterConfig {
    @Bean("InfAlarmDataSource")
    @Qualifier("InfAlarmDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.wgmaster")
    public DataSource getInfAlarmDataSource(){
        DruidDataSource datasource = new DruidDataSource();
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    @Bean("InfAlarmSqlSessionFactory")
    public SqlSessionFactory hiveSqlSessionFactory(@Qualifier("InfAlarmDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //设置mybaties的mapper.xml所在位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/Wgmaster/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean("InfAlarmSqlSessionTemplate")
    public SqlSessionTemplate InfAlarmSqlSessionTemplate(@Qualifier("InfAlarmSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
